import _ from 'lodash';

import { layerCreator, buildingUtils, layerUtils, jsapi } from '@/utils/arcgis';

export default {
  namespace: 'layerlist',

  state: {
    /**
     * 记录当前地图上已经加载的图层key
     */
    layerAdded: [],

    checkedKeys: [],

    treenodeArr: [],

    configCheck:[],

    configExpand:[],

    arrBimids:[],

    nowClickBim: null,

    searchBimMap: '',
  },

  effects: {
    *addLayer({ payload }, { put, call, select }) {
      if (!window.agsGlobal || !window.agsGlobal.view) {
        return Promise.resolve(false)
      }

      if (payload && payload.length) {
        const addedKeys = yield select(store => store.layerlist.layerAdded);
        yield put({
          type: 'updateAddedLayerKeys',
          payload: [...addedKeys, ...payload],
        });

        let result = [];
        let layers = [];
        for (let i = 0; i < payload.length; i += 1) {
          layerCreator
            .create({
              type: 'building',
              url: payload[i].url,
              // title: `${payload[i].name}${payload[i].version || ''}`,
              title: `${payload[i].name}`,
            })
            .then(lyr => {
              lyr.id = `bim-layer-${payload[i].objectid}`;
              layers.push(lyr);
              window.agsGlobal.view.map.add(lyr);
              buildingUtils.setFullModel(lyr);
              console.log(lyr)
              layerUtils.zoomTolayer(window.agsGlobal.view, lyr);
              result.push(true)
            });


        }
        return new Promise((resolve, reject) => {
          const timer = setInterval(() => {
            if (payload.length===result.length) {

              resolve(true)
              clearInterval(timer)
            }
          }, 3000);
        })
      }
    },

    *removeLayer({ payload }, { put, call, select }) {
      if (!window.agsGlobal || !window.agsGlobal.view) {
        return;
      }

      if (payload && payload.length) {
        const addedKeys = yield select(store => store.layerlist.layerAdded);
        let newArr = addedKeys;
        _.each(payload, p => {
          const index = addedKeys.findIndex(d => d.objectid === p.objectid);
          if (index > -1) {
            newArr = [..._.slice(newArr, 0, index), ..._.slice(newArr, index + 1)];
          }
        });

        yield put({
          type: 'updateAddedLayerKeys',
          payload: newArr,
        });

        for (let i = 0; i < payload.length; i += 1) {
          const lyr = window.agsGlobal.view.map.findLayerById(`bim-layer-${payload[i].objectid}`);
          if (lyr) {
            window.agsGlobal.view.map.remove(lyr);
          }
        }
      }
    }
  },

  reducers: {
    updateAddedLayerKeys(state, action) {
      return { ...state, layerAdded: action.payload };
    },
    updateCheckedKeys(state, action) {
      return { ...state, checkedKeys: action.payload };
    },
    upsearchBimMap(state, action) {
      return { ...state, searchBimMap: action.payload };
    },
    setTreenodeArr(state, action) {
      return { ...state, treenodeArr: action.payload };
    },
    setConfigCheck(state, action) {
      return { ...state, configCheck: action.payload };
    },
    setConfigExpand(state, action) {
      return { ...state, configExpand: action.payload };
    },
    setArrBimids(state, action) {
      return { ...state, arrBimids: action.payload };
    },
    setNowClickBim(state, action) {
      return { ...state, nowClickBim: action.payload };
    },
  },
};
